﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class Text : Component, IDrawableComponent
    {
        private String mValue = "";
        private SpriteFont mFont = null;        
        private Vector2 mScale = Vector2.One;        
        private Color mColour = Color.White;
        private SpriteEffects mEffects = SpriteEffects.None;
        private Boolean mVisible = true;
        private Boolean mInCameraView = true;
        private Single mLayerDepth = 0.9f;                
        
        /// <summary>
        /// The actual text to be written.
        /// </summary>
        public String Value { get { return mValue; } set { mValue = value; } }

        /// <summary>
        /// The Font to use for the writing.
        /// </summary>
        public SpriteFont Font { get { return mFont; } set { mFont = value; } }

        /// <summary>
        /// The colour used for the Text component when drawing.
        /// </summary>
        public Color Colour { get { return mColour; } set { mColour = value; } }

        /// <summary>
        /// The SpriteEffects that you would like applied to the Text.
        /// </summary>
        public SpriteEffects Effects { get { return mEffects; } set { mEffects = value; } }

        /// <summary>
        /// The layer depth of the Text component, between 1 (highest) and 0 (lowest).
        /// </summary>
        public Single LayerDepth { get { return mLayerDepth; } set { mLayerDepth = value; } }

        /// <summary>
        /// Gets or sets a value indicating whether this Text component should be drawn.
        /// </summary>
        public Boolean Visible { get { return mVisible; } set { mVisible = value; } }

        /// <summary>
        /// Gets or sets a value indicating if this Text component is currently in view of the active camera. Do not adjust manually.
        /// </summary>
        public Boolean InCameraView { get { return mInCameraView; } set { mInCameraView = value; } }        

        /// <summary>
        /// Create a new instance of a Text component.
        /// </summary>
        /// <param name="gameObject"></param>
        public Text(GameObject gameObject)
            : base(gameObject)
        {
        }

        /// <summary>
        /// Updates this Text component.
        /// </summary>
        public override void Update()
        {
            //if(mFont != null)
              //  GameObject.Transform.Size = mFont.MeasureString(mValue);

            base.Update();
        }

        /// <summary>
        /// Draws this Text component.
        /// </summary>
        /// <param name="spriteBatch">The SpriteBatch object to use for the drawing.</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (mFont == null)
                return;

            if (Visible && InCameraView)
            {
                Vector2 origin = mFont.MeasureString(mValue) / 2;
                spriteBatch.DrawString(mFont, mValue, GameObject.Transform.Position, mColour, GameObject.Transform.RotationRadians, origin, mScale, mEffects, mLayerDepth);
            }
        }
    }
}
